const axios = require('axios');

// 配置
const BASE_URL = 'http://localhost:3000/api';

// 测试获取随机短剧
async function testGetRandomShorts() {
  try {
    console.log('🎲 获取随机短剧（包含章节信息）...');
    const response = await axios.get(`${BASE_URL}/shorts/random`);
    console.log('✅ 随机短剧:', JSON.stringify(response.data, null, 2));
    
    // 检查是否包含章节信息
    if (response.data && response.data.length > 0) {
      const firstShort = response.data[0];
      console.log(`📺 第一个短剧: ${firstShort.title}`);
      console.log(`📚 章节数量: ${firstShort.chapters ? firstShort.chapters.length : 0}`);
      if (firstShort.chapters && firstShort.chapters.length > 0) {
        console.log(`🎬 第一章: ${firstShort.chapters[0].title}`);
        console.log(`⏱️ 第一章时长: ${firstShort.chapters[0].duration}分钟`);
        console.log(`💎 第一章所需积分: ${firstShort.chapters[0].points}`);
      }
    }
    
    return response.data;
  } catch (error) {
    console.error('❌ 获取随机短剧失败:', error.response?.data || error.message);
  }
}

// 测试获取指定数量的随机短剧
async function testGetRandomShortsWithCount(count = 3) {
  try {
    console.log(`🎲 获取${count}个随机短剧...`);
    const response = await axios.get(`${BASE_URL}/shorts/random?count=${count}`);
    console.log(`✅ ${count}个随机短剧:`, JSON.stringify(response.data, null, 2));
    return response.data;
  } catch (error) {
    console.error('❌ 获取随机短剧失败:', error.response?.data || error.message);
  }
}

// 测试获取指定分类的随机短剧
async function testGetRandomShortsByCategory(categoryId = 1, count = 5) {
  try {
    console.log(`🎲 获取分类${categoryId}的${count}个随机短剧...`);
    const response = await axios.get(`${BASE_URL}/shorts/random?categoryId=${categoryId}&count=${count}`);
    console.log(`✅ 分类${categoryId}的${count}个随机短剧:`, JSON.stringify(response.data, null, 2));
    return response.data;
  } catch (error) {
    console.error('❌ 获取分类随机短剧失败:', error.response?.data || error.message);
  }
}

// 测试获取免费随机短剧
async function testGetRandomFreeShorts(count = 5) {
  try {
    console.log(`🎲 获取${count}个免费随机短剧...`);
    const response = await axios.get(`${BASE_URL}/shorts/random?isFree=true&count=${count}`);
    console.log(`✅ ${count}个免费随机短剧:`, JSON.stringify(response.data, null, 2));
    return response.data;
  } catch (error) {
    console.error('❌ 获取免费随机短剧失败:', error.response?.data || error.message);
  }
}

// 测试获取指定方向的随机短剧
async function testGetRandomShortsByDirection(directionId = 1, count = 5) {
  try {
    console.log(`🎲 获取方向${directionId}的${count}个随机短剧...`);
    const response = await axios.get(`${BASE_URL}/shorts/random?directionId=${directionId}&count=${count}`);
    console.log(`✅ 方向${directionId}的${count}个随机短剧:`, JSON.stringify(response.data, null, 2));
    return response.data;
  } catch (error) {
    console.error('❌ 获取方向随机短剧失败:', error.response?.data || error.message);
  }
}

// 主测试函数
async function runTests() {
  console.log('🚀 开始测试随机短剧接口...\n');

  // 1. 获取默认随机短剧（5个）
  await testGetRandomShorts();
  console.log('');

  // 2. 获取指定数量的随机短剧
  await testGetRandomShortsWithCount(3);
  console.log('');

  // 3. 获取指定分类的随机短剧
  await testGetRandomShortsByCategory(1, 4);
  console.log('');

  // 4. 获取免费随机短剧
  await testGetRandomFreeShorts(3);
  console.log('');

  // 5. 获取指定方向的随机短剧
  await testGetRandomShortsByDirection(1, 4);
  console.log('');

  console.log('✨ 测试完成！');
}

// 运行测试
runTests().catch(console.error); 